package LinkList;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: LinkList
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-02  20:17
 * @Description: TODO
 * @Version: 1.0
 */
public class 两两交换链表中的节点24 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 设置虚拟节点
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        ListNode curr=dummy;
        while (curr.next!=null&&curr.next.next!=null){
            ListNode future=curr.next;
            ListNode next=curr.next.next;
            curr.next=next;
            future.next=next.next;
            next.next=future;
            curr=future;
        }
        return dummy.next;
    }
}
